﻿using System;
using System.Collections.Generic;
using System.Text;
using DBHelper;

namespace SyncCommon.DAL
{
    public partial class SyncDB : Database
    {
        public SyncDB()
            : base("Data Source=SyncDB.db;Pooling=true;FailIfMissing=false", "System.Data.SQLite")
        {
            CommonConstruct();
        }

        partial void CommonConstruct();

        public interface IFactory
        {
            SyncDB GetInstance();
        }

        public static IFactory Factory { get; set; }
        public static SyncDB GetInstance()
        {
            if (_instance != null)
                return _instance;

            if (Factory != null)
                return Factory.GetInstance();
            else
                return new SyncDB();
        }

        [ThreadStatic]
        static SyncDB _instance;

        public override void OnBeginTransaction()
        {
            if (_instance == null)
                _instance = this;
        }

        public override void OnEndTransaction()
        {
            if (_instance == this)
                _instance = null;
        }

        public class Record<T> where T : new()
        {
            public static SyncDB repo { get { return SyncDB.GetInstance(); } }
            public bool IsNew() { return repo.IsNew(this); }
            public object Insert() { return repo.Insert(this); }
            public int Update(IEnumerable<string> columns) { return repo.Update(this, columns); }
            public static int Update(string sql, params object[] args) { return repo.Update<T>(sql, args); }
            public static int Update(Sql sql) { return repo.Update<T>(sql); }
            public int Delete() { return repo.Delete(this); }
            public static int Delete(string sql, params object[] args) { return repo.Delete<T>(sql, args); }
            public static int Delete(Sql sql) { return repo.Delete<T>(sql); }
            public static int Delete(object primaryKey) { return repo.Delete<T>(primaryKey); }
            public static bool Exists(object primaryKey) { return repo.Exists<T>(primaryKey); }
            public static T SingleOrDefault(object primaryKey) { return repo.SingleOrDefault<T>(primaryKey); }
            public static T SingleOrDefault(string sql, params object[] args) { return repo.SingleOrDefault<T>(sql, args); }
            public static T SingleOrDefault(Sql sql) { return repo.SingleOrDefault<T>(sql); }
            public static T FirstOrDefault(string sql, params object[] args) { return repo.FirstOrDefault<T>(sql, args); }
            public static T FirstOrDefault(Sql sql) { return repo.FirstOrDefault<T>(sql); }
            public static T Single(object primaryKey) { return repo.Single<T>(primaryKey); }
            public static T Single(string sql, params object[] args) { return repo.Single<T>(sql, args); }
            public static T Single(Sql sql) { return repo.Single<T>(sql); }
            public static T First(string sql, params object[] args) { return repo.First<T>(sql, args); }
            public static T First(Sql sql) { return repo.First<T>(sql); }
            public static List<T> Fetch(string sql, params object[] args) { return repo.Fetch<T>(sql, args); }
            public static List<T> Fetch(Sql sql) { return repo.Fetch<T>(sql); }
            public static List<T> Fetch(long page, long itemsPerPage, string sql, params object[] args) { return repo.Fetch<T>(page, itemsPerPage, sql, args); }
            public static List<T> Fetch(long page, long itemsPerPage, Sql sql) { return repo.Fetch<T>(page, itemsPerPage, sql); }
            public static List<T> SkipTake(long skip, long take, string sql, params object[] args) { return repo.SkipTake<T>(skip, take, sql, args); }
            public static List<T> SkipTake(long skip, long take, Sql sql) { return repo.SkipTake<T>(skip, take, sql); }
            public static Page<T> Page(long page, long itemsPerPage, string sql, params object[] args) { return repo.Page<T>(page, itemsPerPage, sql, args); }
            public static Page<T> Page(long page, long itemsPerPage, Sql sql) { return repo.Page<T>(page, itemsPerPage, sql); }
            public static IEnumerable<T> Query(string sql, params object[] args) { return repo.Query<T>(sql, args); }
            public static IEnumerable<T> Query(Sql sql) { return repo.Query<T>(sql); }
            public static List<T> FindAll() { return repo.Query<T>(""); }
            private Dictionary<string, bool> ModifiedColumns;
            private void OnLoaded()
            {
                ModifiedColumns = new Dictionary<string, bool>();
            }
            protected void MarkColumnModified(string column_name)
            {
                if (ModifiedColumns != null)
                    ModifiedColumns[column_name] = true;
            }
            public int Update()
            {
                if (ModifiedColumns == null)
                    return repo.Update(this);

                int retv = repo.Update(this, ModifiedColumns.Keys);
                ModifiedColumns.Clear();
                return retv;
            }
            public void Save()
            {
                if (repo.IsNew(this))
                    repo.Insert(this);
                else
                    Update();
            }
        }
    }



    [TableName("HousePost")]
    [PrimaryKey("ID")]
    [ExplicitColumns]
    public partial class HousePost : SyncDB.Record<HousePost>
    {
        [Column]
        public Int64 ID
        {
            get
            {
                return _ID;
            }
            set
            {
                _ID = value;
                MarkColumnModified("ID");
            }
        }
        Int64 _ID;

        [Column]
        public string HefaCode
        {
            get
            {
                return _HefaCode;
            }
            set
            {
                _HefaCode = value;
                MarkColumnModified("HefaCode");
            }
        }
        string _HefaCode;

        [Column]
        public string HouseInfo
        {
            get
            {
                return _HouseInfo;
            }
            set
            {
                _HouseInfo = value;
                MarkColumnModified("HouseInfo");
            }
        }
        string _HouseInfo;

        [Column]
        public Int64? TradeType
        {
            get
            {
                return _TradeType;
            }
            set
            {
                _TradeType = value;
                MarkColumnModified("TradeType");
            }
        }
        Int64? _TradeType;

        [Column]
        public string TargetID
        {
            get
            {
                return _TargetID;
            }
            set
            {
                _TargetID = value;
                MarkColumnModified("TargetID");
            }
        }
        string _TargetID;

        [Column]
        public DateTime LastTime
        {
            get
            {
                return _LastTime;
            }
            set
            {
                _LastTime = value;
                MarkColumnModified("LastTime");
            }
        }
        DateTime _LastTime;

        [Column]
        public Int64? WebSiteID
        {
            get
            {
                return _WebSiteID;
            }
            set
            {
                _WebSiteID = value;
                MarkColumnModified("WebSiteID");
            }
        }
        Int64? _WebSiteID;

        [Column]
        public string Var1
        {
            get
            {
                return _Var1;
            }
            set
            {
                _Var1 = value;
                MarkColumnModified("Var1");
            }
        }
        string _Var1;

        [Column]
        public string Var2
        {
            get
            {
                return _Var2;
            }
            set
            {
                _Var2 = value;
                MarkColumnModified("Var2");
            }
        }
        string _Var2;

        [Column]
        public bool? IsSucess
        {
            get
            {
                return _IsSucess;
            }
            set
            {
                _IsSucess = value;
                MarkColumnModified("IsSucess");
            }
        }
        bool? _IsSucess;



        [Column]
        public string Status
        {
            get
            {
                return _Status;
            }
            set
            {
                _Status = value;
                MarkColumnModified("Status");
            }
        }
        string _Status;

        [Column]
        public string Url
        {
            get
            {
                return _Url;
            }
            set
            {
                _Url = value;
                MarkColumnModified("Url");
            }
        }
        string _Url;


        [Column]
        public Int64? RequestType
        {
            get
            {
                return _RequestType;
            }
            set
            {
                _RequestType = value;
                MarkColumnModified("RequestType");
            }
        }
        Int64? _RequestType;

    }

    [TableName("WebLogin")]
    [PrimaryKey("ID")]
    [ExplicitColumns]
    public partial class WebLogin : SyncDB.Record<WebLogin>
    {
        [Column]
        public Int64 ID
        {
            get
            {
                return _ID;
            }
            set
            {
                _ID = value;
                MarkColumnModified("ID");
            }
        }
        Int64 _ID;

        [Column]
        public Int64 WebSiteID
        {
            get
            {
                return _WebSiteID;
            }
            set
            {
                _WebSiteID = value;
                MarkColumnModified("WebSiteID");
            }
        }
        Int64 _WebSiteID;

        [Column]
        public string WebSiteName
        {
            get
            {
                return _WebSiteName;
            }
            set
            {
                _WebSiteName = value;
                MarkColumnModified("WebSiteName");
            }
        }
        string _WebSiteName;

        [Column]
        public string UserName
        {
            get
            {
                return _UserName;
            }
            set
            {
                _UserName = value;
                MarkColumnModified("UserName");
            }
        }
        string _UserName;

        [Column]
        public string Password
        {
            get
            {
                return _Password;
            }
            set
            {
                _Password = value;
                MarkColumnModified("Password");
            }
        }
        string _Password;


        [Column]
        public bool? IsValid
        {
            get
            {
                return _IsValid;
            }
            set
            {
                _IsValid = value;
                MarkColumnModified("IsValid");
            }
        }
        bool? _IsValid;

        [Column]
        public bool? IsStart
        {
            get
            {
                return _IsStart;
            }
            set
            {
                _IsStart = value;
                MarkColumnModified("IsStart");
            }
        }
        bool? _IsStart;

        [Column]
        public string Var1
        {
            get
            {
                return _Var1;
            }
            set
            {
                _Var1 = value;
                MarkColumnModified("Var1");
            }
        }
        string _Var1;

        [Column]
        public string Var2
        {
            get
            {
                return _Var2;
            }
            set
            {
                _Var2 = value;
                MarkColumnModified("Var2");
            }
        }
        string _Var2;

    }
}
